package hhh.com.dianming;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class find extends AppCompatActivity {
    ListView listView;
    private TextView tv_name,tv_user;
    private List<Map<String,String>> list;
    private MyAdapter adapter;
    private MySqlite mySqlite;
    private SQLiteDatabase db;
    SharedPreferences sp;
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find);
        listView = (ListView) findViewById(R.id.lv_user);
        sp = getSharedPreferences("user",MODE_PRIVATE);
        editor = sp.edit();
        list = new ArrayList<>();
        getData();
        adapter = new MyAdapter(
                this,
                list,
                R.layout.item,
                new String[]{"name","user"},
                new int[]{R.id.tv_name,R.id.tv_user}
        );

        listView.setAdapter(adapter);

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                deleteMing(list.get(i).get("id"));
                Toast.makeText(find.this, "删除成功", Toast.LENGTH_SHORT).show();
                list.remove(i);
                adapter.notifyDataSetChanged();
                return true;
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                editor.putString("id",list.get(i).get("id"));
                editor.putString("name",list.get(i).get("name"));
                editor.putString("user", list.get(i).get("user"));
                editor.commit();
                Intent intent = new Intent(find.this,Into_alt.class);
                startActivity(intent);
            }
        });
    }

    public void deleteMing(String i){
        mySqlite=new MySqlite(this);
        db=mySqlite.getWritableDatabase();
        db.delete("mings","id=?",new String[]{i});
        db.close();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
        adapter = new MyAdapter(
                this,
                list,
                R.layout.item,
                new String[]{"name","user"},
                new int[]{R.id.tv_name,R.id.tv_user}
        );

        listView.setAdapter(adapter);
    }



    public List<Map<String,String>> getData(){
        list.clear();
        MySqlite mySQLite = new MySqlite(this);
        SQLiteDatabase database = mySQLite.getReadableDatabase();
        Cursor cursor = database.rawQuery("select * from mings", null);
        System.out.println(cursor.getCount());
        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String user = cursor.getString(cursor.getColumnIndex("user"));
            Map<String,String> map = new HashMap<>();
            map.put("id",id);
            map.put("name",name);
            map.put("user",user);
            list.add(map);
        }
        return list;
    }

    private class MyAdapter extends SimpleAdapter {
        public MyAdapter(Context context, List<? extends Map<String, ?>> data, int resource,
                         String[] from, int[] to) {
            super(context, data, resource, from, to);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            tv_name = v.findViewById(R.id.tv_name);
            tv_user = v.findViewById(R.id.tv_user);
            tv_name.setText(list.get(position).get("name"));
            tv_user.setText(list.get(position).get("user"));
            return v;
        }
    }
}
